package com.vayne.injectdemo;

import com.vayne.bean.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * @author xuerui.zhang
 * @since 2020/4/13 下午9:22
 */
public class AnnotationFiledInjectionDemo {

    // 2、这里是把容器中的 Bean 对象赋值给我们的私有字段
    @Autowired
    private CourseHolder courseHolder;

    // {@link AutowiredAnnotationBeanPostProcessor.class}
    // @Autowired 会忽略掉静态字段，只针对于实例字段或者对象字段
    @Autowired
    private static CourseHolder courseHolder2;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(AnnotationFiledInjectionDemo.class);

        XmlBeanDefinitionReader beanDefinition = new XmlBeanDefinitionReader(applicationContext);
        String path = "classpath:/dependency_lookup_config.xml";
        beanDefinition.loadBeanDefinitions(path);

        applicationContext.refresh();
        AnnotationFiledInjectionDemo demo = applicationContext.getBean(AnnotationFiledInjectionDemo.class);
        System.out.println(demo.courseHolder);
        System.out.println(demo.courseHolder2);
        applicationContext.close();
    }

    // 1、这是是往容器中注册一个 CourseHolder bean 实例
    @Bean
    public CourseHolder courseHolder(Course course) {
        return new CourseHolder(course);
    }
}
